#!/usr/bin/python2.4

# pylint: disable-msg=C6304

"""A vector field specialized for the entrance to JDF for the 2011 OOR.

This file is obsolete.  It has been replaced by extract_xtide.py,
which goes to the xtide server directly, and reads the tables from that,
rather than having hard coded values here.
"""

import time

from matrix import DM
from matrix import LatLon
from matrix import Polar
import vectorfield


class XtideJDF(vectorfield.VectorField):
  """A Vector field modelling currents at entrance to JDF."""

  def __init__(self):
    super(XtideJDF, self).__init__('jdf_xtide')
    self._latlon = LatLon(DM(48, 27.0), -DM(124, 35.0))
    self._minlatlon = self._latlon
    self._maxlatlon = self._latlon
    self._angle_flood = 115  # from the bottom of the table
    self._angle_ebb = 290  # from the bottom of the table

    self._date_string = None  # passed from one call of Day to nex calls of Time

  def Day(self, date_string):
    """Capture the date string from the table."""
    self._date_string = date_string

  def Time(self, time_string, value):
    """Capture the time string and signed current magnitude.

    Args:
      time_string: A 4 digit string formatted %H%M.
      value: A signed value, where negative values are ebbs, + floods.

    Returns:
      The CoordsToVector item added to the VectorField.
    """
    self._time_string = time_string
    x = time.strptime(self._date_string + ' ' + self._time_string,
                      '%a %d %b %Y %H%M')
    unix_epoch_time = time.mktime(x)

    if value < 0:
      angle = self._angle_ebb
    else:
      angle = self._angle_flood

    value = Polar(abs(value), angle)

    cv = vectorfield.CoordsToVector(unix_epoch_time, self._latlon, value)

    self.AddItem(cv)
    return cv

  def OOR2011(self):
    """Load with values manually converted from rows in xtide html output.

    This is for the 2011 Oregon Offshore Race.

    See:
      http://www.dairiki.org/tides/monthly.php/jdf/2011-05
    """
    self.Day('Tue 10 May 2011')
    self.Time('1347', -1.4)
    self.Time('1752', 0.0)
    self.Time('2032', 0.7)
    self.Time('2309', 0.0)

    self.Day('Wed 11 May 2011')
    self.Time('0318', -1.4)
    self.Time('0837', 0.0)
    self.Time('0919', 0.1)
    self.Time('1001', 0.0)
    self.Time('1503', -1.2)
    self.Time('1900', 0.0)
    self.Time('2129', 0.6)
    self.Time('2353', 0.0)

    self.Day('Thu 12 May 2011')
    self.Time('0411', -1.6)
    self.Time('0852', 0.0)
    self.Time('1031', 0.3)
    self.Time('1212', 0.0)
    self.Time('1620', -1.2)
    self.Time('2008', 0.0)
    self.Time('2226', 0.6)

    self.Day('Fri 13 May 2011')
    self.Time('0038', 0.0)
    self.Time('0502', -1.8)
    self.Time('0924', 0.0)
    self.Time('1135', 0.6)
    self.Time('1351', 0.0)
    self.Time('1732', -1.2)
    self.Time('2115', 0.0)
    self.Time('2321', 0.5)

    self.Day('Sat 14 May 2011')
    self.Time('0121', 0.0)
    self.Time('0550', -2.0)
    self.Time('1002', 0.0)
    self.Time('1232', 0.9)
    self.Time('1510', 0.0)
    self.Time('1836', -1.3)
    self.Time('2218', 0.0)

    self.Day('Sun 15 May 2011')
    self.Time('0014', 0.5)
    self.Time('0204', 0.0)
    self.Time('0638', -2.2)  # Ebb, picked for a test case
    self.Time('1042', 0.0)  # Slack, picked for upper bound of a test case
    self.Time('1325', 1.1)
    self.Time('1617', 0.0)
    self.Time('1935', -1.4)

    self.BuildModelFromAllData()
